<?php defined('SYSPATH') or die('No direct script access.');
/**
 * Created by PhpStorm.
 * Author: daisc
 * QQ: 2444329889
 * Time: 2017/12/26 16:30
 * Desc: desc
 */

class  Controller_Pc_Contract extends Stourweb_Controller
{

    function before()
    {
        parent::before();
    }



    public function action_download()
    {
        $ordersn = Common::remove_xss($this->params['ordersn']);
        $tourer_id = $_GET['tourer_id'];
        $url = $GLOBALS['cfg_basehost'].'/contract/view/ordersn/'.$ordersn.'/headhidden/1?tourer_id='.$tourer_id;
        $html = file_get_contents($url);
        $order = DB::select('contract_id','typeid')->from('member_order')
            ->where('ordersn','=',$ordersn)->execute()->current();
        $contract = Model_Contract::get_contents($order['contract_id'],$order['typeid']);

        include(DOCROOT . '/tools/lib/mpdf/mpdf.php');
        $mpdf = new mPDF('zh-CN','A4','','',15,15,38,28,15,15);

      //  $mpdf->useAdobeCJK = true;
        $mpdf->showWatermarkText = true;
        $mpdf->autoLangToFont = true;
        $mpdf->WriteHTML($html);
        $mpdf->Output($GLOBALS['cfg_webname'].$contract['title'].'.pdf','I');
        exit;
    }


    public function action_view()
    {
        $ordersn = Common::remove_xss($this->params['ordersn']);
        $headhidden = Common::remove_xss($this->params['headhidden']);
        $order = Model_Member_Order::order_info($ordersn);

//        $tourer_id = $_GET['tourer_id'];

        if(!in_array($order['status'],array(2,5)))
        {
            Common::head404();
        }
        $order = Model_Contract::format_order_data($order);
        $info = DB::select()->from('contract')
            ->where('id','=',$order['contract_id'])->execute()
            ->current();

        if(!$info['partyBid']||$info['partyBid']<=0) {
            $info['partyBid']=Model_Sysconfig::get_configs(0,'cfg_default_partyB',1);
        }
        $config=  Model_Contract::get_contract_config($info['partyBid']);
        $headerUrl=DB::select()
            ->from('weblist')
            ->where('id', '=',1)
            ->execute()
            ->current();
        $config['seal']=$headerUrl['weburl'].$config['seal'];
        if(!$info['partyBid']) {
            $list=Model_Contract::get_contract_config(false);
            $config=$list[0];
        }

        $tourer= null;
        if($order['typeid']=='114' && St_Functions::is_system_app_install(114)) {
            $tourer = DB::select()->from('outdoor_tourer')
//                ->where('id','=',$tourer_id)
                ->and_where('orderid','=',$order['id'])
                ->execute()
                ->as_array();
        }
        else
        {
            $tourer = DB::select()
                ->from('member_order_tourer')
//                ->where('id','=',$tourer_id)
                ->and_where('orderid','=',$order['id'])
                ->execute()
                ->as_array();
        }


//        if($info['partyBid']){
//            $contract_partybt = DB::select()->from('contract_partyb')->where('id', '=', $info['partyBid'])->execute()->current();
//            $headerUrl=DB::select()->from('sline_weblist')
//                ->where('id', '=',1)
//                ->execute()
//                ->current();
//            $contract_partybt['seal']=$headerUrl['weburl'].$contract_partybt['seal'];
//        }

        //收费标准
        if($order['people4_num']){
            $people4_name = Model_Sysconfig::get_configs(0,'cfg_line_people4_name');
            $this->assign('people4_name',$people4_name['cfg_line_people4_name']);
        }
        if($order['people5_num']){
            $people5_name = Model_Sysconfig::get_configs(0,'cfg_line_people5_name');
            $this->assign('people5_name',$people5_name['cfg_line_people5_name']);
        }
        if($order['people6_num']){
            $people6_name = Model_Sysconfig::get_configs(0,'cfg_line_people6_name');
            $this->assign('people6_name',$people6_name['cfg_line_people6_name']);
        }

        $this->assign('config',$config);
        $this->assign('order',$order);
        $this->assign('info',$info);
        $this->assign('tourer',$tourer);
        $this->assign('headhidden',$headhidden);
        $this->display('contract/view');

    }

    public function action_book_view()
    {
        $contract_id = intval($this->params['contract_id']);
        $this->assign('contract_id',$contract_id);
        $info = DB::select()->from('contract')
            ->where('id','=',$contract_id)->execute()
            ->current();
        if(!$info['partyBid']||$info['partyBid']<=0)
        {
            $info['partyBid']=Model_Sysconfig::get_configs(0,'cfg_default_partyB',1);
        }
        $config=  Model_Contract::get_contract_config($info['partyBid']);
        if(!$info['partyBid'])
        {
            $list=Model_Contract::get_contract_config(false);
            $config=$list[0];
        }
        $this->assign('config',$config);
        $this->assign('info',$info);
        $this->assign('headhidden',1);
        $this->display('contract/view');
    }

    //订单完成后生成合同信息
    public function action_create_contract()
    {
        $orderno = Common::remove_xss(Arr::get($_GET, 'ordersn'));
        if($orderno){
            //订单信息
            $memberOrder = DB::select()->from('member_order')->where('ordersn', '=', $orderno)->execute()->current();
            if($memberOrder && $memberOrder['contract_id']!=0)
            {
                //获得游客信息
                $tourist=DB::select()->from('member_order_tourer')->where('orderid','=',$memberOrder['id'])->execute()->as_array();
                //合同模板信息
                $contract=DB::select()->from('contract')->where('id','=',$memberOrder['contract_id'])->execute()->current();
                //产品信息
                $product=DB::select()->from('line')->where('id','=',$memberOrder['productautoid'])->execute()->current();
                $linesuit=Model_Line_Suit::suit_info($memberOrder['suitid']);
                $suitpric=DB::select()->from('line_suit_price')
                    ->where('day', '=', strtotime($memberOrder['usedate']))
                    ->and_where('suitid', '=', $linesuit['id'])
                    ->and_where('supplier_channel_id', '=', 1)
                    ->execute()->current();
                //保险信息
                //$insurance=Model_Insurance_Base::get_order_insur($memberOrder['id']);
                $insurance = array();
                $orderinfo = DB::select()->from('member_order')->where('pid','=',$memberOrder['id'])->execute()->current();
                if($orderinfo)
                {

                    $insurance['title'] = $orderinfo['productname'];
                    $insurance['dingnum'] = $orderinfo['dingnum'];
                    $insurance['price'] = $orderinfo['price'];
                    $insurance['usedate'] = $orderinfo['usedate'];
                }

                if($contract && $contract['type']==2)//12301合同
                {
                    $contract_data=json_decode($contract['content_parameter'],true);
                    $contract_data['travelAgency']=json_encode($contract_data['travelAgency']);
                    $contract_data['signatory']=json_encode($contract_data['signatory']);
                    $contract_data['dispute']=json_encode($contract_data['dispute']);
                    $contract_data['groupAgreement']=json_encode($contract_data['groupAgreement']);
                    $contract_data['tourGuides']=json_encode($contract_data['tourGuides']);
                    $contract_data['typeid']=$memberOrder['typeid'];
                    $contract_data['contract_id']=$contract['id'];
                    $contract_data['order_id']=$memberOrder['id'];
                    $contract_data['contractNumber']='';
                    $contract_data['contractnName']=$memberOrder['productname'];
                    $contract_data['addtime']=time();
                    $contract_data['status']=0;
                    $contract_data['partyBid']=$contract['partyBid'];
                    $contract_data['callbackURL']=$contract_data['callbackURL'];
                    //游客信息
                    $contract_data['touristsInfo']=array();
                    $contract_data['touristsInfo']['totalNumber']=intval($memberOrder['dingnum'])+intval($memberOrder['childnum'])+intval($memberOrder['oldnum'])+intval($memberOrder['people4_num'])+intval($memberOrder['people5_num'])+intval($memberOrder['people6_num']);
                    $contract_data['touristsInfo']['adultNumber']=intval($memberOrder['dingnum'])+intval($memberOrder['people4_num'])+intval($memberOrder['people5_num'])+intval($memberOrder['people6_num']);
                    $contract_data['touristsInfo']['childNumber']=intval($memberOrder['childnum']);
                    $contract_data['touristsInfo']['emergencyContactName']=$memberOrder['linkman'];
                    $contract_data['touristsInfo']['emergencyContactPhone']=$memberOrder['linktel'];
                    $contract_data['touristsInfo']['tourists']=array();
                    if(!empty($tourist))
                    {
                        $config = Model_Sysconfig::get_configs(0,array('cfg_contract_signed'));
                        foreach ($tourist as $kt=>$vt)
                        {
                            $ctourist=array();
                            $ctourist['number']=$kt+1;
                            if($config['cfg_contract_signed']=='first_tourer' && $kt==0)
                            {
                                $ctourist['isSigner']=1;
                            }
                            else if($config['cfg_contract_signed']=='all_tourer')
                            {
                                $ctourist['isSigner']=1;
                            }
                            else if($config['cfg_contract_signed']=='hand'){
                                $ctourist['isSigner']=0;
                            }else{
                                $ctourist['isSigner']=0;
                            }
                            $ctourist['name']=$vt['tourername'];
                            $ctourist['gender']=$vt['sex'];
                            $ctourist['nationality']=$vt['nationality'];
                            $ctourist['phone']=$vt['mobile'];
                            $ctourist['address']='';
                            $ctourist['ID']['IDNumber']=$vt['cardnumber'];
                            switch ($vt['cardtype'])
                            {
                                case '身份证':
                                    $ctourist['ID']['IDType']=1;
                                    break;
                                case '士官证':
                                    $ctourist['ID']['IDType']=2;
                                    break;
                                case '港澳通行证':
                                    $ctourist['ID']['IDType']=3;
                                    break;
                                case '护照':
                                    $ctourist['ID']['IDType']=4;
                                    break;
                                case '赴台证':
                                    $ctourist['ID']['IDType']=5;
                                    break;
                                case '回乡证':
                                    $ctourist['ID']['IDType']=6;
                                    break;
                                case '台胞证':
                                    $ctourist['ID']['IDType']=7;
                                    break;
                                case '其他':
                                    $ctourist['ID']['IDType']=8;
                                    break;
                                default:
                                    $ctourist['ID']['IDType']=1;
                                    break;
                            }
                            array_push($contract_data['touristsInfo']['tourists'],$ctourist);
                        }
                    }else{
                        $ctourist=array();
                        $ctourist['number']=0;
                        $ctourist['isSigner']=0;
                        $ctourist['name']='';
                        $ctourist['gender']='';
                        $ctourist['nationality']='';
                        $ctourist['phone']='';
                        $ctourist['address']='';
                        $ctourist['ID']['IDNumber']='';
                        $ctourist['ID']['IDType']=1;
                        array_push($contract_data['touristsInfo']['tourists'],$ctourist);
                    }
                    $contract_data['touristsInfo']=json_encode($contract_data['touristsInfo']);
                    //行程部分
                    if($product['isstyle'])
                    {
                        switch ($product['isstyle'])
                        {
                            case '2':
                                $contract_data['inputType']=0;
                                break;
                            case '1':
                                $contract_data['inputType']=1;
                                break;
                        }
                    }else{
                        $contract_data['inputType']=0;
                    }
                    $lineday=$product['lineday']?$product['lineday']:0;
                    $contract_data['itinerary']['startDate']=$memberOrder['usedate'];
                    $contract_data['itinerary']['endDate']=date('Y-m-d',strtotime("+{$lineday} day",strtotime($memberOrder['usedate'])));
                    $contract_data['itinerary']['days']=$lineday;
                    $contract_data['itinerary']['nights']=$product['linenight']?$product['linenight']:0;
                    $contract_data['itinerary']['routeName']=$memberOrder['productname'];
                    $contract_data['itinerary']=json_encode($contract_data['itinerary']);
                    //费用部分
                    $orderInfo = Model_Member_Order::order_info($memberOrder['ordersn']);
                    $contract_data['cost']['adultCost']=$suitpric['adultprice'];
                    $contract_data['cost']['childCost']=$suitpric['childprice'];
                    $contract_data['cost']['singleSupplementCost']=$suitpric['roombalance'];
                    $contract_data['cost']['totalCost']=$orderInfo['payprice'];
                    $contract_data['cost']['paymentMethod']=3;
                    $contract_data['cost']=json_encode($contract_data['cost']);
                    //保险部分
                    if($insurance)
                    {
                        $contract_data['insurance']['purchaseMethod']=1;
                        $contract_data['insurance']['productName']=$insurance['title'];
                        $contract_data['insurance']['company']='';
                    }else{
                        $contract_data['insurance']['purchaseMethod']=1;
                        $contract_data['insurance']['productName']='';
                        $contract_data['insurance']['company']='';
                    }
                    $contract_data['insurance']=json_encode($contract_data['insurance']);
                    //自费项目部分
                    $contract_data['activities']['date']='';
                    $contract_data['activities']['place']='';
                    $contract_data['activities']['item']='';
                    $contract_data['activities']['fee']=0;
                    $contract_data['activities']['stayDuration']='';
                    $contract_data['activities']['memo']='';
                    $contract_data['activities']=json_encode($contract_data['activities']);
                    //购物部分
                    $contract_data['hasShopping']=1;
                    $contract_data['shoppingViewSpot']='';
                    $contract_data['shoppings']['date']=date('Y-m-d',time());
                    $contract_data['shoppings']['place']='';
                    $contract_data['shoppings']['shoppingPlace']='';
                    $contract_data['shoppings']['goods']='';
                    $contract_data['shoppings']['stayDuration']='';
                    $contract_data['shoppings']['memo']='';
                    $contract_data['shoppings']=json_encode($contract_data['shoppings']);
                }
            }
        }
        $contract_data=$contract_data?$contract_data:[];
        $result= DB::insert('contract_supervise',array_keys($contract_data))->values(array_values($contract_data))->execute();
        $id='';
        list($id)=$result;
        $tresult=Model_Dispose::online_reporting_contract($id);
        if($result)
        {
            echo true;
        }else{
            echo false;
        }
        //echo json_encode($contract_data);
    }

    //12301合同接收回调方法
    public function action_get_contract()
    {
        $body = file_get_contents("php://input");
        $data=json_decode($body,true);
        if($data['data']['contractNumber'])
        {
            $supervise=DB::select()->from('contract_supervise')->where('contractNumber','=',$data['data']['contractNumber'])->execute()->current();
            if($supervise)
            {
                if($data['data']['state']){
                    $cdata['status']=$data['data']['state'];
                    if($data['data']['content'])
                    {
                        $cdata['callbackstr']=$supervise['callbackstr'].'|'.$data['data']['content'];
                    }
                    DB::update('contract_supervise')->set($cdata)->where('id','=',$supervise['id'])->execute();
                }
            }
        }
        echo 'success';
    }
}